package org.skatiger.jrove.spring.form.renderer;

import java.io.IOException;

import org.skatiger.jrove.ResponseWriter;
import org.skatiger.jrove.ViewContext;
import org.skatiger.jrove.component.UIComponent;
import org.springframework.util.ObjectUtils;
import org.springframework.web.servlet.support.BindStatus;

public class ErrorsRenderer extends AbstractFormElementRenderer {
    @Override
    public void encodeBegin(ViewContext context, UIComponent component)
            throws IOException {
        super.encodeBegin(context, component);
        ResponseWriter writer = context.getResponseWriter();
        String element = (String) component.getAttributes().get("element");
        String delimiter = (String) component.getAttributes().get("delimiter");
        writer.startElement(element, component);
        renderId(context, component);
        String _delimiter = ObjectUtils.getDisplayString(delimiter);
        BindStatus bindStatus = getBindStatus(component);
        String[] errorMessages = bindStatus.getErrorMessages();
        for (int i = 0; i < errorMessages.length; i++) {
            
            String errorMessage = errorMessages[i];
            if (i > 0) {
                writer.write(_delimiter);
            }
            
            writer.writeText(errorMessage, null);
        }
        writer.endElement(element);

    }
}
